package com.topnav.mc.admin.view.device;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.topnav.mc.admin.entity.Device;
import com.topnav.mc.admin.security.PermissionGrade;
import com.topnav.mc.admin.view.user.UserView;
import com.topnav.mc.base.CommServiceImpl;
import com.topnav.mc.base.CommViewServiceImpl;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class DeviceViewServiceImpl extends CommViewServiceImpl<DeviceViewMapper, DeviceView> implements IDeviceViewService {

    /**
     * 由实体类生成查询条件
     * 并返回QueryWrapper
     * @param queryWrapper
     * @return
     */
    @Override
    public QueryWrapper<DeviceView> genWrapper(QueryWrapper<DeviceView> queryWrapper){
        PermissionGrade permissionGrade =  this.getPermissionGrade();

        if (queryWrapper.getEntity()==null){
            if (permissionGrade != null){
                if(permissionGrade.getPermissionLevel() == 1){
                    queryWrapper.eq("user_id",permissionGrade.getUserId());
                }else if(permissionGrade.getPermissionLevel() == 2){
                    queryWrapper.eq("org_id",permissionGrade.getOrgId());
                }else if(permissionGrade.getPermissionLevel() == 3 || permissionGrade.getPermissionLevel() == 4){
                    queryWrapper.like("path",permissionGrade.getOrgPath());
                }
            }
            return queryWrapper;
        }

        QueryWrapper<DeviceView> wrapper = new QueryWrapper<DeviceView>();
        if (permissionGrade != null){
            if(permissionGrade.getPermissionLevel() == 1){
                wrapper.eq("user_id",permissionGrade.getUserId());
            }else if(permissionGrade.getPermissionLevel() == 2){
                wrapper.eq("org_id",permissionGrade.getOrgId());
            }else if(permissionGrade.getPermissionLevel() == 3 || permissionGrade.getPermissionLevel() == 4){
                wrapper.like("path",permissionGrade.getOrgPath());
            }
        }
        DeviceView entity = queryWrapper.getEntity();
        if (entity.getId()!=null){
            wrapper.eq("id", entity.getId());
        }
        if (entity.getCate()!=null){
            wrapper.eq("cate", entity.getCate());
        }

        if (entity.getType()!=null){
            wrapper.eq("type", entity.getType());
        }
        if (entity.getOrgId()!=null){
            wrapper.eq("org_id", entity.getOrgId());
        }
        if (entity.getUserId()!=null){
            wrapper.eq("user_id", entity.getUserId());
        }
        if (entity.getGbServerId()!=null){
            wrapper.eq("gb_server_id", entity.getGbServerId());
        }

        if (!StringUtils.isEmpty(entity.getCode())){
            wrapper.like("code", entity.getCode());
        }
        if (!StringUtils.isEmpty(entity.getName())){
            wrapper.like("name", entity.getName());
        }
        return wrapper;
    }
}
